package bb.lanxing.lib.devices.bryton.bbcp;

import bb.lanxing.lib.devices.sprint.Commands;
import java.nio.ByteBuffer;
import java.util.ArrayList;

class BbcpDataInputStream implements BbcpInputStream {
    private BbcpCommand mCommand;
    protected byte[] mData;
    private short mDataSizeInPackets;
    private byte mLastFragmentSize;
    private int mBytesRead = -1;
    private int mRewindIndex = -1;
    private ArrayList<ByteBuffer> mCachedPackages = new ArrayList<>();

    public BbcpDataInputStream(byte[] bArr, BbcpCommand bbcpCommand) {
        this.mData = bArr;
        this.mCommand = bbcpCommand;
    }

    @Override
    public byte[] readPackage() throws BbcpValidationException {
        byte[] bArr;
        int i = this.mRewindIndex;
        if (i != -1 && i < this.mCachedPackages.size()) {
            bArr = this.mCachedPackages.get(this.mRewindIndex).array();
            this.mRewindIndex++;
        } else {
            int i2 = this.mBytesRead;
            if (i2 == -1) {
                byte[] bArr2 = this.mData;
                short length = (short) (bArr2.length / 18);
                byte length2 = (byte) (bArr2.length % 18);
                this.mLastFragmentSize = length2;
                if (length2 != 0) {
                    length = (short) (length + 1);
                } else {
                    this.mLastFragmentSize = Commands.FILE_NOT_FOUNT;
                }
                this.mDataSizeInPackets = length;
                this.mBytesRead = 0;
                bArr = BbcpData.createStartFragment(length, this.mLastFragmentSize, this.mCommand.getId(), this.mCommand.getSeqNo()).pack();
            } else {
                byte[] bArr3 = this.mData;
                if (i2 == bArr3.length) {
                    bArr = BbcpData.createEndFragment((short) (this.mDataSizeInPackets + 1), bArr3).pack();
                    this.mBytesRead++;
                } else if (i2 < bArr3.length) {
                    int length3 = bArr3.length - i2 < 18 ? bArr3.length - i2 : 18;
                    byte[] bArr4 = new byte[length3];
                    System.arraycopy(bArr3, i2, bArr4, 0, length3);
                    int i3 = this.mBytesRead + length3;
                    this.mBytesRead = i3;
                    short s = (short) (i3 / 18);
                    if (i3 % 18 != 0) {
                        s = (short) (s + 1);
                    }
                    bArr = BbcpData.createDataFragment(s, bArr4).pack();
                } else {
                    bArr = null;
                }
            }
            if (bArr != null) {
                this.mCachedPackages.add(ByteBuffer.wrap(bArr));
            }
        }
        if (bArr != null || this.mBytesRead >= this.mData.length) {
            return bArr;
        }
        throw new BbcpValidationException("read package filed");
    }

    @Override
    public short getDataFragmentCount() {
        return this.mDataSizeInPackets;
    }

    @Override
    public byte getLastDataFragmentSize() {
        return this.mLastFragmentSize;
    }

    @Override
    public void mark() {
        this.mCachedPackages.clear();
        this.mRewindIndex = -1;
    }

    @Override
    public void rewind() {
        this.mRewindIndex = 0;
    }

    @Override
    public void close() {
        this.mData = null;
        this.mCachedPackages.clear();
        this.mCachedPackages = null;
    }
}
